﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using BCL.Entity.Generic.DAL;
using BCL.Entity.Entity;
using NazSoft.Framework;

public partial class Admin_AddResult : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //RadioButton1.Attributes.Add("onclick", "Show()");
        //RadioButton2.Attributes.Add("onclick", "Show()");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SaveExeclFile();

        DataTable dt = SaveInDataBase();

        int i = ResultType();

        SaveReults(dt, i);
        File.Delete(Session["filename"].ToString());
        Label1.Text = "Completed";
        Label1.Visible = true;
        Button1.Enabled = false;

    }

    private DataTable SaveInDataBase()
    {
        string strConn;
        strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Excel 8.0; Extended Properties=HDR=No; IMEX=1;Data Source=" + Session["filename"].ToString();

        //You must use the $ after the object you reference in the spreadsheet
        OleDbDataAdapter myCommand = new OleDbDataAdapter(@"SELECT * FROM [" + txtTablename.Text.ToString() + "$]", strConn);

        DataSet myDataSet = new DataSet();
        myCommand.Fill(myDataSet, "ExcelInfo");

        myCommand.Dispose();
        //myCommand.
        return myDataSet.Tables[0];
        //DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
        // DataGrid1.DataBind();
    }

    private void SaveExeclFile()
    {
        string strPath = Server.MapPath("~/TempEx");
        FileInfo fi = new FileInfo(FileUpload1.FileName);

        if (FileUpload1.HasFile)
        {
            strPath = strPath + @"\" + FileUpload1.FileName;
            FileUpload1.SaveAs(strPath);
            Session["filename"] = strPath;
        }
    }

    private int ResultType()
    {
        //DataClassesDataContext ds = new DataClassesDataContext();
        //int s;
        //if(DropDownList1.SelectedIndex ==0)
        //    s=0;
        //else
        //    s =int.Parse( DropDownList1.SelectedItem.Value.ToString());

        //ds.ResultTypeInsert(DateTime.Now, txtTitle.Text.ToString(), s, true);

        //select Id
        //var v = null;// ds.ResultTypeLastIDSelect().Single();

        return 0;


    }

    private void SaveReults(DataTable dt, int ID)
    {
        string strTextMessage;
        ResultsShowGenericDAL dal = new ResultsShowGenericDAL();
        Result_Type rType = null;
        ResultsShow resultsshow = null;
        for (int i = 1; i < dt.Rows.Count; i++)
        {

            rType = new Result_Type();

            resultsshow = new ResultsShow();

            rType.Title = dt.Rows[i].ItemArray[0].ToString();
            rType.ResultAnnocementDate = Convert.ToDateTime(dt.Rows[i].ItemArray[1]);
            DateTime d = new DateTime(Convert.ToInt32(dt.Rows[i].ItemArray[2]), 1, 1);
            rType.ResultYear = d;
            rType.ResultLevel = dt.Rows[i].ItemArray[3].ToString();



            resultsshow.RollNo = dt.Rows[i].ItemArray[4].ToString();

            resultsshow.ObtainNumber = Convert.ToInt32(dt.Rows[i].ItemArray[5].ToString());
            resultsshow.Remarks = dt.Rows[i].ItemArray[6].ToString();
            resultsshow.TotalNumber = Convert.ToInt32(dt.Rows[i].ItemArray[7].ToString());
            resultsshow.NumberOfPaper = Convert.ToInt32(dt.Rows[i].ItemArray[9].ToString());

            resultsshow.IsActive = dt.Rows[i].ItemArray[11].ToString() == "YES" ? true : false;

            strTextMessage = dt.Rows[i].ItemArray[10].ToString();

            strTextMessage = strTextMessage.Replace("{a}", dt.Rows[i].ItemArray[0].ToString());
            strTextMessage = strTextMessage.Replace("{b}", dt.Rows[i].ItemArray[1].ToString());
            strTextMessage = strTextMessage.Replace("{c}", dt.Rows[i].ItemArray[2].ToString());
            strTextMessage = strTextMessage.Replace("{d}", dt.Rows[i].ItemArray[3].ToString());
            strTextMessage = strTextMessage.Replace("{e}", dt.Rows[i].ItemArray[4].ToString());
            strTextMessage = strTextMessage.Replace("{f}", dt.Rows[i].ItemArray[5].ToString());
            strTextMessage = strTextMessage.Replace("{g}", dt.Rows[i].ItemArray[6].ToString());
            strTextMessage = strTextMessage.Replace("{h}", dt.Rows[i].ItemArray[7].ToString());
            strTextMessage = strTextMessage.Replace("{i}", dt.Rows[i].ItemArray[8].ToString());
            strTextMessage = strTextMessage.Replace("{j}", dt.Rows[i].ItemArray[9].ToString());
            strTextMessage = strTextMessage.Replace("{per}", ((resultsshow.ObtainNumber * 100) / resultsshow.TotalNumber).ToString());
            //strTextMessage = strTextMessage.Replace("{d}", dt.Rows[i].ItemArray[3].ToString());


            resultsshow.DisplayText = strTextMessage;
            dal.Save(resultsshow, "KhurramAli Khan", rType);

        }


    }

}

